package com.sunshine.student_register.database;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.sunshine.student_register.database.register.RegisterDao;
import com.sunshine.student_register.database.register.RegisterEntity;
import com.sunshine.student_register.database.student.StudentDao;
import com.sunshine.student_register.database.student.StudentEntity;

/**
 * @author sunshine
 * @date 2020/5/30
 * 学生签到数据库
 * 这是一个抽象类，用于生成和获取数据库
 * 静态方法和静态变量为了保证数据库效率和不同时占用
 * 上锁保证一致性
 * 具体操作由抽象方法通过获取各个表的Dao来实现
 * @see StudentDao
 * @see RegisterDao
 */

@Database(entities = {StudentEntity.class, RegisterEntity.class}, version = 1, exportSchema = false)

public abstract class StudentRegisterDatabase extends RoomDatabase {

    private static StudentRegisterDatabase database;

    static synchronized StudentRegisterDatabase getDatabase(Context context){
        if (null == database){
            database = Room.databaseBuilder(context.getApplicationContext(), StudentRegisterDatabase.class, "student_register.db")
                    .allowMainThreadQueries()
                    .build();
        }
        return database;
    }

    public abstract StudentDao getStudentDao();

    public abstract RegisterDao getRegisterDao();
}
